package com.aaa.service.impl;

import com.aaa.entity.Staff;
import com.aaa.mapper.StaffMapper;
import com.aaa.service.StaffService;
import com.aaa.vo.R;
import com.aaa.vo.StaffVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service // 表示当前类为service层 并且交于spring管理该类的创建 以及类中属性注入
public class StaffServiceImpl implements StaffService {
    @Autowired
    private StaffMapper staffMapper;
    @Autowired
    private HttpSession session;

    // 前后端分离: 前端要求后端返回的数据格式必须统一。
    @Override
    public R findByNameAndPwd(String name, String pwd) {
        Staff staff = staffMapper.selectByAccountAndPwd(name, pwd);
        if (staff == null) {
            return new R(500, "账号或密码错误", null);
        }
        if (staff.getState() != 1) {
            return new R(500, "账号状态异常,请联系管理员.", null);
        }
        //表示登录成功
        //把信息存在session中.
        session.setAttribute("staff", staff);
        return new R(200, "登录成功", null);
    }

    @Override
    public R findByCondition(Integer currentPage, Integer pageSize, StaffVo staffVo) {
        //开启分页
        PageHelper.startPage(currentPage, pageSize);
        //调用dao查询功能
        Staff staff = (Staff) session.getAttribute("staff");
        if(staff.getStaffid()!=111){ //不是超级管理员
            staffVo.setBranchId(staff.getBranchid());
        }
        List<Staff> list = staffMapper.selectByCondition(staffVo);
        //把查询的结果封装到pageInfo
        PageInfo<Staff> pageInfo = new PageInfo<>(list);
        return new R(200, "查询成功", pageInfo);
    }

    @Override
    public R checkStaffId(String staffId) {
        Staff staff= staffMapper.selectByStaffId(staffId);
        return new R(200,"查询成功",staff);
    }

    @Override
    public R insert(Staff staff) {
        staff.setState(1);
        int row= staffMapper.insertSelective(staff);
        if(row>0){
            return new R(200,"添加成功",null);
        }else{
            return new R(500,"添加失败",null);
        }
    }
}





















